package com.aidong.dao.wills;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.model.wills.PssLossOrder;
import com.aidong.model.wills.PssLossOrderList;
import com.aidong.model.wills.PssLossOrderTotal;
import com.aidong.model.wills.PssProductStock;
import com.aidong.model.wills.PssProductStockPK;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.springframework.stereotype.Repository;

@Repository
public class PssLossOrderDao extends BaseDao<PssLossOrder, Integer> {
    public PssLossOrderDao() {
    }

    public void getLstLossOrder(Pagination<PssLossOrderTotal> pagination, ConditionMap cd) {
        ArrayList params = new ArrayList();
        String sqlw = cd.toSql(params);
        String sql = "select a.LossShop, b.ProductCode, c.Unit, sum(b.Quantity) as qty \nfrom PssLossOrder a inner join PssLossOrderList b on a.LossId=b.LossId \nleft join PssProductCode c on b.ProductCode=c.ProductCode \n" + (sqlw.length() > 0?"where " + sqlw:"");
        sql = sql + "  group by a.LossShop,b.ProductCode,c.Unit \nhaving sum(b.Quantity)<>0 \n  order by a.LossShop,b.ProductCode \n";
        this.queryPageListBySql(PssLossOrderTotal.class, pagination, sql, params.toArray());
    }

    public void getLstLossOrderDetailList(Pagination<PssLossOrderTotal> pagination, ConditionMap cd) {
        ArrayList params = new ArrayList();
        String sqlw = cd.toSql(params);
        String sql = "select a.LossShop, a.LossDate, a.CreateUser, a.Note, b.ProductCode, c.Unit, b.Quantity as qty \nfrom PssLossOrder a inner join PssLossOrderList b on a.LossId=b.LossId \nleft join PssProductCode c on b.ProductCode=c.ProductCode \n" + (sqlw.length() > 0?"where " + sqlw:"") + "  order by a.LossShop,b.ProductCode \n";
        this.queryPageListBySql(PssLossOrderTotal.class, pagination, sql, params.toArray());
    }

    public void addLossGainProduct(PssLossOrder pssLossOrder, List<PssLossOrderList> pssLossOrderList, PssProductStockDao pssProductStockDao) {
        this.add(pssLossOrder);
        Iterator i$ = pssLossOrderList.iterator();

        while(i$.hasNext()) {
            PssLossOrderList pss = (PssLossOrderList)i$.next();
            pss.setLossId(pssLossOrder.getLossId());
            this.getSession().save(pss);
            this.updateStock(pssProductStockDao, pssLossOrder.getLossShop(), pss.getProductCode(), pss.getQuantity(), pss.getCreateUser());
        }

    }

    private void updateStock(PssProductStockDao pssProductStockDao, String shopCode, String productCode, Double quantity, String createUser) {
        PssProductStockPK pssProductStockPK = new PssProductStockPK();
        pssProductStockPK.setProductCode(productCode);
        pssProductStockPK.setShopCode(shopCode);
        PssProductStock pssProductStock = (PssProductStock)pssProductStockDao.getById(pssProductStockPK);
        if(pssProductStock == null) {
            pssProductStock = new PssProductStock();
            pssProductStock.setProductCode(productCode);
            pssProductStock.setShopCode(shopCode);
            pssProductStock.setQuantity(Double.valueOf(-quantity.doubleValue()));
            pssProductStock.setCreateUser(createUser);
            pssProductStock.setCreateTime(new Date());
            pssProductStock.setUpdateTime(new Date());
            pssProductStockDao.add(pssProductStock);
        } else {
            pssProductStock.setQuantity(Double.valueOf(pssProductStock.getQuantity().doubleValue() - quantity.doubleValue()));
            pssProductStock.setUpdateTime(new Date());
            pssProductStockDao.update(pssProductStock);
        }

    }
}
